package com.example.opengldemo._026_;

import android.opengl.GLES30;

import com.example.opengldemo._06_._6MySurfaceView;
import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderUtil;
import com.example.opengldemo.util.ShapeFactory;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;

public class BallDemo {

    int mProgram ;

    int m_A_PositionHandle ;

    int m_U_MVPMatrixHandle ;
    int m_U_MMatrixHandle;
    int m_U_RHandle ;

    int m_A_NormalHandle;
    int m_A_LightLocationHandle;
    int m_A_CameraLocationHandle;
    int m_A_LightDirectionHandle;


    FloatBuffer vertaxBuffer ;
    FloatBuffer fragBuffer ;
    FloatBuffer mNormalBuffer;

    int vCount ;

    float xAngle ;
    float yAngle ;
    float zAngle ;

    float r = 0.8f;
    final float UNIT_SIZE = 0.2f;

    BallDemo(_026_MySurfaceView view){
        initBufferData();
        initHandle(view) ;
    }

    private void initHandle(_026_MySurfaceView view) {
        mProgram = ShaderUtil.createProgram(
                ShaderUtil.loadFromAssetsFile("suntwo/vertex2.sh" , view.getResources()) ,
                ShaderUtil.loadFromAssetsFile("suntwo/fragment2.sh" , view.getResources())
        );

        m_A_PositionHandle = GLES30.glGetAttribLocation(mProgram , "aPosition");
        m_A_NormalHandle = GLES30.glGetAttribLocation(mProgram, "aNormal");

        m_U_MVPMatrixHandle = GLES30.glGetUniformLocation(mProgram , "uMVPMatrix");
        m_U_MMatrixHandle = GLES30.glGetUniformLocation(mProgram , "uMMatrix"); //世界空间的位置
        m_U_RHandle = GLES30.glGetUniformLocation(mProgram , "uR") ;
        m_A_LightLocationHandle=GLES30.glGetUniformLocation(mProgram, "uLightLocation");
        m_A_CameraLocationHandle = GLES30.glGetUniformLocation(mProgram , "uCamera");
        m_A_LightDirectionHandle=GLES30.glGetUniformLocation( mProgram, "uLightDirection");
    }

    private void initBufferData() {
        float[] vertexArray = ShapeFactory.getBallArrayData(r , 10);
        ByteBuffer byteBuffer = ByteBuffer.allocateDirect(vertexArray.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        vertaxBuffer = byteBuffer.asFloatBuffer();
        vertaxBuffer.put(vertexArray);
        vertaxBuffer.position(0);
        vCount = vertexArray.length/3 ;
        float[] colorArray = getFragData();
        byteBuffer = ByteBuffer.allocateDirect(colorArray.length * 4);
        byteBuffer.order(ByteOrder.nativeOrder());
        fragBuffer = byteBuffer.asFloatBuffer();
        fragBuffer.put(colorArray);
        fragBuffer.position(0);

        byteBuffer = ByteBuffer.allocateDirect(vertexArray.length*4);
        byteBuffer.order(ByteOrder.nativeOrder());
        mNormalBuffer = byteBuffer.asFloatBuffer();
        mNormalBuffer.put(vertexArray);
        mNormalBuffer.position(0);

    }



    private float[] getFragData() {
        float[] colorArray = new float[vCount * 4];//顶点着色数据的初始化
        for (int i = 0; i < vCount; i++) {
            if (i % 3 == 0) { //中心点为白色，RGBA 4 个通道[1,1,1,0]
                colorArray[i * 4] = 1;
                colorArray[i * 4 + 1] = 1;
                colorArray[i * 4 + 2] = 1;
                colorArray[i * 4 + 3] = 0;
            } else { //边上的点为淡蓝色，RGBA 4 个通道[0.45,0.75,0.75,0]
                colorArray[i * 4] = 0.45f;
                colorArray[i * 4 + 1] = 0.75f;
                colorArray[i * 4 + 2] = 0.75f;
                colorArray[i * 4 + 3] = 0;
            }
        }
        return colorArray;
    }


    public void drawSelf(){
        MatrixState.rotate(xAngle, 1, 0, 0);
        MatrixState.rotate(yAngle, 0, 1, 0);
        MatrixState.rotate(zAngle, 0, 0, 1);

        GLES30.glUseProgram(mProgram);
        GLES30.glUniform1f(m_U_RHandle, r * UNIT_SIZE);//将半􏱋属性传入渲染管线
        GLES30.glUniformMatrix4fv(m_U_MVPMatrixHandle , 1 , false , MatrixState.getFinalMatrix() , 0);
        GLES30.glUniformMatrix4fv(m_U_MMatrixHandle , 1 , false , MatrixState.getMMatrix() , 0);
        GLES30.glUniform3fv(m_A_CameraLocationHandle, 1, MatrixState.cameraLocationFB);
        GLES30.glVertexAttribPointer(m_A_PositionHandle , 3 , GLES30.GL_FLOAT , false , 3*4 , vertaxBuffer);
        GLES30.glUniform3fv( m_A_LightLocationHandle , 1, MatrixState.lightLocationFB);
        GLES30.glUniform3fv(m_A_LightDirectionHandle, 1, MatrixState.lightDirectionFB);
        GLES30.glVertexAttribPointer(m_A_NormalHandle, 3, GLES30.GL_FLOAT, false,3 * 4, mNormalBuffer);

        GLES30.glEnableVertexAttribArray(m_A_PositionHandle);
        GLES30.glEnableVertexAttribArray(m_A_NormalHandle);

        GLES30.glDrawArrays(GLES30.GL_TRIANGLES , 0  ,vCount );

        GLES30.glDisableVertexAttribArray(m_A_PositionHandle);
        GLES30.glDisableVertexAttribArray(m_A_NormalHandle);

    }







}
